package com.shujia.mr.DianXin3;

import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.lib.input.FileSplit;

import java.io.IOException;
//81101,北京市
//<0L,"D55433A437AEC8D8D3DB2BCA56E9E64392A9D93C-83401-8340104,26">
//<0L,"81101,北京市">
//<"81101","北京市">
//<"81101","">

public class DianXinMapper3 extends Mapper<LongWritable, Text, Text, Text> {
    @Override
    protected void map(LongWritable key, Text value, Mapper<LongWritable, Text, Text, Text>.Context context) throws IOException, InterruptedException {
        String line = value.toString();
        Long date = 0L;
        String cityId = null;
        String cityName = null;
        String phoneNumber = null;
        FileSplit inputSplit = (FileSplit) context.getInputSplit();
        String fileName = inputSplit.getPath().getName();
        if (fileName.startsWith("city")) {
            String[] infos = line.split(",");
            cityId = infos[0];
            cityName = infos[1];
            context.write(new Text(cityId), new Text("#" + cityName));
        } else if (fileName.startsWith("part")) {
            String[] infos = line.split("\t");
            String[] info = infos[0].split("-");
            cityId = info[1];
            phoneNumber = info[0];
            if ("".equals(infos[1]) || infos[1] == null) {
                infos[1] = "0";
            }
            date = Long.valueOf(infos[1]);
            context.write(new Text(cityId), new Text("*" + phoneNumber + "-" + date));
        }
    }
}
